package com.ponhu.ea2.goodsCenter.provider.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author luyu
 * @create 2022-03-23 13:39
 */
@Configuration
@Slf4j
public class RabbitMQConfig {

    @Value("${spring.rabbitmq.username}")
    private String username;
    @Value("${spring.rabbitmq.password}")
    private String password;
    @Value("${spring.rabbitmq.addresses}")
    private String addresses;
    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;
    @Value("${spring.rabbitmq.publisher-confirms}")
    private Boolean publisherConfirms;
    @Value("${spring.rabbitmq.publisher-returns}")
    private Boolean publisherReturns;

    @Bean
    public CachingConnectionFactory cachingConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(addresses);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPublisherConfirms(publisherConfirms);
        connectionFactory.setPublisherReturns(publisherReturns);
        return connectionFactory;
    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public DirectExchange newExchange(){
        return new DirectExchange("goods.sync");
    }
    @Bean
    public Queue priceQueue(){
        return new Queue("price.data",true);
    }
    @Bean
    public Queue goodsDataQueue(){
        return new Queue("goods.data",true);
    }
    @Bean
    public Queue GoodsStatusQueue(){
        return new Queue("goods.status",true);
    }
    @Bean
    public Binding bindingpriceQueue(Queue priceQueue,DirectExchange newExchange){
        return BindingBuilder.bind(priceQueue).to(newExchange).with("ph.price.data");
    }
    @Bean
    public Binding bindingGoodsDataQueue(Queue goodsDataQueue,DirectExchange newExchange){
        return BindingBuilder.bind(goodsDataQueue).to(newExchange).with("ph.goods.data");
    }
    @Bean
    public Binding bindingGoodsStatusQueue(Queue GoodsStatusQueue,DirectExchange newExchange){
        return BindingBuilder.bind(GoodsStatusQueue).to(newExchange).with("ph.goods.status");
    }

    @Bean
    public FanoutExchange newGoodsPriceFanoutExchange(){
        return new FanoutExchange("goods.price.sync.fanout", true, false);
    }

    @Bean
    public FanoutExchange newGoodsInfoFanoutExchange(){
        return new FanoutExchange("goods.data.sync.fanout", true, false);
    }


    /*@Bean
    public Queue anonymousQueue() {
        //注意：必须使用匿名队列才能达到本文目标，此匿名队列实例的名称在消费端会被使用
        //return new AnonymousQueue();
        return new Queue("price.data.fanout", false, false, true);
    }

    @Bean
    public Binding bindAnonymousQueue() {
        return BindingBuilder.bind(anonymousQueue()).to(newGoodsPriceFanoutExchange());
    }
*/
}
